<template>
  <!--  //交强险询价单节点-->
  <el-form :model="ruleForm" :rules="rules" ref="ruleForm" :inline="false"
           :label-position="labelPosition" :class="isEdit?'':'formItem'">
    <!--交强险信息-->
    <el-card class="box-card" shadow="never">

      <div slot="header" class="header-card">
        <span class="header-card-title">交强险信息</span>
      </div>

      <!--询价单审批节点-->
      <el-row :gutter="24">
        <el-col :span="8">
          <el-form-item label="租赁禀议保险公司:" v-if="isShow" prop="rentInsuranceCompany">
            <el-input v-model="ruleForm.rentInsuranceCompany" v-if="isEdit" disabled></el-input>
            <span v-else>{{ ruleForm.rentInsuranceCompany }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="投保渠道:" prop="insuranceChannels" v-if="isShow">
            <el-select v-model="ruleForm.insuranceChannels" v-if="isEdit" style="width: 100%;" disabled>
              <el-option v-for="v in dict.type.insurance_channels" :key="v.value" :label="v.label"
                         :value="Number(v.value)">
              </el-option>
            </el-select>
            <span v-else>{{ selectDictLabel(dict.type.insurance_channels, ruleForm.insuranceChannels) }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="交强险公司:" prop="jqxCompany" v-if="isShow">
            <insurance-select businessTypeCode="5" placeholder="请选择交强险公司"
                              :companyId="ruleForm.jqxCompanyId"
                              v-if="isEdit" @change="handleChange"/>
            <span v-else>{{ ruleForm.jqxCompany }}</span>
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="交强险公司编码:" prop="jqxCompanyCode" v-if="isShow">
            <el-input v-model="ruleForm.jqxCompanyCode" disabled v-if="isEdit"
                      placeholder="请输入交强险公司编码"></el-input>
            <span v-else>{{ ruleForm.jqxCompanyCode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="交强险性质:" prop="jqxInsuranceType" v-if="isShow">
            <el-select v-model="ruleForm.jqxInsuranceType"  v-if="isEdit" style="width: 100%;"
                       @change="handleType"
                       placeholder="请选择交强险性质">
              <el-option label="运营" value="1">
              </el-option>
              <el-option label="非运营" value="2">
              </el-option>
            </el-select>
            <span v-else>
              {{ ruleForm.jqxInsuranceType == 1 ? '运营' : ruleForm.jqxInsuranceType == 2 ? '非运营' : '' }}
            </span>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="交强险起保日:" prop="jqxExpectBeginTime" v-if="isShow">
            <el-date-picker v-model="ruleForm.jqxExpectBeginTime" type="date" placeholder="请选择交强险起保日"
                            v-if="isEdit"
                            @change="handleDate"
                            value-format='yyyy-MM-dd' style="width: 100%;">
            </el-date-picker>
            <span v-else>{{ ruleForm.jqxExpectBeginTime }}</span>
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="车船税是否缴纳:" prop="carTaxStatus" v-if="isShow">
            <el-radio-group v-model="ruleForm.carTaxStatus" v-if="isEdit" style="width: 100%;">
              <el-radio :label="1">是</el-radio>
              <el-radio :label="2">否</el-radio>
            </el-radio-group>
            <span v-else>{{
                ruleForm.carTaxStatus == '1' ? '是' : ruleForm.carTaxStatus == '2' ? '否' : ""
              }}</span>
          </el-form-item>
        </el-col>

      </el-row>
    </el-card>
  </el-form>
</template>

<script>
import {effectDate} from "@/utils/libs/effectDate.js"

export default {
  name: 'jqxInquiryReview',//交强险询价单节点
  dicts: ["insurance_channels"],
  props: {
    value: {
      type: Object,
      default: {},
    },
    state: {
      type: String,
      default: "",
    }
  },
  components: {
    insuranceSelect: () => import('../insuranceSelect')
  },
  computed: {
    ruleForm: {
      get() {
        let that = this;
        const proxy = new Proxy(that.value, {
          get(target, key) {
            return Reflect.get(target, key);
          },
          set(target, key, value) {
            if (value) {
              that.handleClearValidate(key)
            }
            that.$emit("input", {...target, [key]: value});
            return true;
          },
        });
        return proxy;
      },
    },
    labelPosition() {
      if (this.state === 'write') {
        return 'top'
      } else {
        return 'right'
      }
    },
    isEdit() {
      let bool;
      switch (this.state) {
        case "write":
          bool = true;
          break;
        case "disabled":
          bool = false;
          break;
        case "readonly":
          bool = false;
          break;
      }
      return bool
    },
  },
  watch: {
    state: {
      handler(val) {

      },
      immediate: true,
      deep: true
    }
  },
  data() {
    var start = (rule, value, callback) => {
      let that = this
      effectDate(rule, value, callback,
          that.ruleForm.jqxExpectBeginTime,
          that.ruleForm.jqxExpectEndTime,
          '交强险起保日',
          '交强险到期日'
      )
    };
    var end = (rule, value, callback) => {
      let that = this
      effectDate(rule, value, callback,
          that.ruleForm.jqxExpectBeginTime,
          that.ruleForm.jqxExpectEndTime,
          '交强险起保日',
          '交强险到期日'
      )
    };
    return {
      isShow: true,
      rules: {
        // rentInsuranceCompany: [
        //   {
        //     required: this.isEdit,
        //     message: "请输入租赁禀议保险公司",
        //     trigger: "blur",
        //   },
        // ],
        // insuranceChannels: [
        //   {required: this.isEdit, message: "请选择投保渠道", trigger: "blur"},
        // ],
        jqxCompany: [
          {
            required: this.state === 'write',
            message: "请选择交强险公司",
            trigger: "blur",
          },
        ],
        jqxPolicyCode: [
          {
            required: this.state === 'write',
            message: "请输入交强险保单号",
            trigger: "blur",
          },
        ],
        jqxCompanyCode: [
          {
            required: this.state === 'write',
            message: "请输入交强险公司编码",
            trigger: "blur",
          },
        ],
        jqxExpectBeginTime: [
          {
            type: "string",
            required: this.state === 'write',
            validator: start,
            trigger: "blur",
          },
        ],
        jqxExpectEndTime: [
          {
            type: "string",
            required: this.state === 'write',
            validator: end,
            trigger: "blur",
          },
        ],
        jqxInsuranceType: [
          {
            required: this.state === 'write',
            message: "请选择交强险性质",
            trigger: "blur",
          },
        ],
        jqxAmount: [
          {
            required: this.state === 'write',
            message: "请输入交强险保费",
            trigger: "blur",
          },
        ],
        carTaxStatus: [
          {
            required: this.state === 'write',
            message: "请选择车船税是否缴纳",
            trigger: "blur",
          },
        ],
        carTax: [
          {required: this.state === 'write', message: "请输入车船税", trigger: "blur"},
        ],
        jqxPolicyUrl: [
          {
            required: this.state === 'write',
            message: "请输入交强险保单",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {

  },
  mounted() {
  },
  methods: {
    handleDate(val){
      this.value.jqxExpectBeginTime = this.ruleForm.jqxExpectBeginTime = val
      this.value.syxExpectBeginTime = this.ruleForm.syxExpectBeginTime = val
    },
    handleType(val) {
      if (val) {
        this.value.jqxInsuranceType = this.ruleForm.jqxInsuranceType = val
        this.value.syxInsuranceType = this.ruleForm.syxInsuranceType = val

      }
    },

    handleShopInsurace() {
      return new Promise((resolve, reject) => {
        this.$refs.ruleForm.validate((valid) => {
          if (valid) {
            resolve();
          } else {
            reject();
          }
        });
      });
    },
    // 店保金额格式
    changeTable(row, key, field) {
      let msg = /^(?:[1-9]\d*|0)(?:\.\d+)?$/;

      if (msg.test(Number(row[field]))) {
        if (Number(row[field]) >= 0) {
          let _value = row[field].toString().match(/^\d+(?:\.\d{0,2})?/)[0];
          row[field] = _value;
        } else {
          row[field] = "";
        }
      } else {
        row[field] = "";
      }
      // this.addAmountNun = this.addAmount(this.detailForm.carMaintainProjectList, 'price')
    },

    changeAmount(str) {
      let msg = /^(?:[1-9]\d*|0)(?:\.\d+)?$/;
      if (!msg.test(this.ruleForm[str])) {
        this.ruleForm[str] = "";
      } else {
        this.ruleForm[str] = this.ruleForm[str]
            .toString()
            .match(/^\d+(?:\.\d{0,2})?/)[0];
      }
    },

    handleChange(val) {
      this.value.jqxCompanyCode = this.ruleForm.jqxCompanyCode = val.code
      this.value.jqxCompany = this.ruleForm.jqxCompany = val.name
      this.value.jqxCompanyId = this.ruleForm.jqxCompanyId = val.id
      this.$refs.ruleForm.clearValidate('jqxCompany')
      this.$refs.ruleForm.clearValidate('jqxCompanyCode')
      this.value.syxCompanyCode = this.ruleForm.syxCompanyCode = val.code
      this.value.syxCompany = this.ruleForm.syxCompany = val.name
      this.value.syxCompanyId = this.ruleForm.syxCompanyId = val.id
    },


    //取消表单效验
    handleClearValidate(str) {
      this.$refs.ruleForm.clearValidate(str);
    },

    valid() {
      return new Promise((resolve) => {
        this.$refs.ruleForm.validate((valid) => {
          resolve(valid);
        });
      });
    },
  },
};
</script>

<style scoped lang="scss">
@import "@/styles/cwgd/formScss.scss";
</style>


